﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


namespace Zen.Toolkit
{
    [ToolboxData("<{0}:Treeview runat=\"server\"></{0}:Treeview>")]
    [ParseChildren(true)]
    [PersistChildren(false)]
    [DojoWidget(ClassMapping = "dijit.Tree", InvokeStartup = false)]
    [DojoRequires(ModuleName = "dijit.Tree")]
    public class Treeview : WidgetBase
    {
        DataStore _store;

        public Treeview() : base(HtmlTextWriterTag.Div) 
        {
            
        }

        protected override void OnInit(EventArgs e)
        {
            _store = Z.FindControl(this, this.StoreId) as DataStore;
            base.OnInit(e);
        }

        [DojoProperty(Name = "model")]
        public string Model
        {
            get { return _store.ModelId; }
        }


        [IDReferenceProperty(typeof(DataStore))]
        public string StoreId
        {
            get
            {
                return (string)this.ViewState["StoreId"] ?? string.Empty;
            }
            set
            {
                this.ViewState["StoreId"] = value;
            }
        }


        ZenEventHandlers<ZenServerEvent> _serverHandlers;

        [DojoServerEvents]
        [Category("Events")]
        [Themeable(false)]
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Description("Event Handlers")]
        public ZenEventHandlers<ZenServerEvent> ServerEvents
        {
            get
            {
                if (this._serverHandlers == null)
                {
                    this._serverHandlers = new ZenEventHandlers<ZenServerEvent>();
                }
                return this._serverHandlers;
            }
        }

        ZenEventHandlers<ZenClientEvent> _clientHandlers;

        [DojoClientEvents]
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Description("Clientside Event Handlers")]
        public ZenEventHandlers<ZenClientEvent> ClientEvents
        {
            get
            {
                if (this._clientHandlers == null)
                {
                    this._clientHandlers = new ZenEventHandlers<ZenClientEvent>();
                }
                return this._clientHandlers;
            }
        }

    }
}
